package com.example.guiguiapplicatio.fragment.record.adapter;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;

import com.example.guiguiapplicatio.R;
import com.example.guiguiapplicatio.databinding.FragmentRecordRecyItemBinding;
import com.example.guiguiapplicatio.placeholder.RecordPlaceholderContent.PlaceholderItem;
import java.util.List;
public class RecordFragmentAdapter extends RecyclerView.Adapter<RecordFragmentAdapter.ViewHolder> {
    private final List<PlaceholderItem> mValues;
    private Context mContext;

    public RecordFragmentAdapter(List<PlaceholderItem> mValues,Context mContext) {
        this.mValues = mValues;
        this.mContext=mContext;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(FragmentRecordRecyItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        // 获取当前位置的占位符项
        PlaceholderItem currentItem = mValues.get(position);
        // 设置 TextView 中的文本
        holder.binding.recordName.setText(currentItem.getName());
        holder.binding.textIdcard.setText(currentItem.getIdcard());
        holder.binding.textDetailedresidence.setText(currentItem.getDetailedresidence());
        holder.binding.recordSex.setText(currentItem.getSex());
        holder.binding.recordAge.setText(String.valueOf(currentItem.getAge()));
        holder.binding.textServicePackage.setText(String.join(",",currentItem.getListenername()));
//        holder.binding.recordSex.setText(currentItem.sex);
        // 根据 id 设置不同的背景和文本
        switch (currentItem.getStatus()) {
            case 0:
                holder.binding.servicerecyStatus.setBackgroundResource(R.drawable.status_label_daishenhe);
                holder.binding.servicerecyStatus.setText("待签约");
                break;
            case 1:
                holder.binding.servicerecyStatus.setBackgroundResource(R.drawable.status_label_daizhifu);
                holder.binding.servicerecyStatus.setText("已签约");
                break;
            case 2:
                holder.binding.servicerecyStatus.setBackgroundResource(R.drawable.status_label_shengxiao);
                holder.binding.servicerecyStatus.setText("已过期");
                break;
            default:
                holder.binding.servicerecyStatus.setBackgroundResource(R.drawable.status_label_bohui);
                holder.binding.servicerecyStatus.setText("其他状态");
                break;
        }
        holder.itemView.clearAnimation();

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String idCard = currentItem.getIdcard();
                Bundle bundle = new Bundle();
                bundle.putString("idCard", idCard);
                // 使用 NavController 导航到目标 fragment，并将 Bundle 作为参数传递
                Navigation.findNavController(v).navigate(R.id.action_recordFragment_to_recordDetailFragment, bundle);
            }
        });
    }



    @Override
    public int getItemCount() {
        int itemCount = mValues.size();
        return itemCount;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        FragmentRecordRecyItemBinding binding;
        public ViewHolder(@NonNull FragmentRecordRecyItemBinding fragmentRecordRecyItemBinding) {
            super(fragmentRecordRecyItemBinding.getRoot());
            this.binding=fragmentRecordRecyItemBinding;
        }
    }
}
